package com.david.flowable.controller;

import com.david.common.utils.PageResult;
import com.david.flowable.pojo.vo.FormFlowVO;
import com.david.flowable.pojo.vo.FormVO;
import com.david.flowable.service.FlowFormService;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.simpleframework.xml.Path;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.david.common.utils.ResultUtils.success;

@RestController
@RequestMapping("/flowable/form")
@Slf4j
public class FlowFormController {
    @Autowired
    private FlowFormService flowFormService;
    /**
    * @Description: 保存设置好的表格
    * @Param: [formVO]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/8/15
    */
    @PostMapping("/save")
    public ResponseEntity<Object> saveForm(@RequestBody FormVO formVO) {
        log.info("保存表格:{}",formVO.getName());
        Integer formId = flowFormService.saveForm(formVO);
        return success(true, formId);
    }

    /**
    * @Description: 保存表格&流程关系
    * @Param: [formFlowVO]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/8/15
    */
    @PutMapping("/formFlow")
    public ResponseEntity<Object> addFormFlow(@RequestBody FormFlowVO formFlowVO){
        log.info("保存表格&流程关系:{}",formFlowVO);
        flowFormService.insertFormFlow(formFlowVO);
        return success(true, "上传成功");
    }

    @PostMapping("/updateFormFlow")
    public ResponseEntity<Object> updateFormFlow(@RequestBody FormFlowVO formFlowVO){
        log.info("修改表格&流程关系:{}",formFlowVO);
        flowFormService.updateFormFlowStatus(formFlowVO);
        return success(true, "修改成功");
    }

    /**
    * @Description: 查询表格&流程关系表
    * @Param: [formFlowVO]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/8/15
    */
    @PostMapping("/formFlow")
    public ResponseEntity<Object> getFormFlow(@RequestBody FormFlowVO formFlowVO){
        log.info("查询表格&流程关系表：page:{},pageSize:{}",formFlowVO.getPage(),formFlowVO.getPageSize());
        PageResult formFlowVOList = flowFormService.selectFormFlow(formFlowVO);
        System.out.println(formFlowVOList);
        return success(true, formFlowVOList);
    }

    /**
    * @Description: 根据表格ID查询表格定义
    * @Param: [formId]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/8/15
    */
    @GetMapping("/get/{formId}")
    public ResponseEntity<Object> getForm(@PathVariable Integer formId){
        log.info("查询表格{}",formId);
        FormVO formVO = flowFormService.getFormById(formId);
        return success(true, formVO);
    }
    /**
    * @Description: 删除表格流程关系、删除表格、流程挂起
    * @Param: [id]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/8/15
    */
    @DeleteMapping("/del/{id}")
    public ResponseEntity<Object> delForm(@PathVariable Integer id){
        log.info("删除表格和流程，关系表ID为{}",id);
        flowFormService.delFormAdnFlow(id);
        return success(true, "删除成功");
    }

    @PutMapping("/changeFormFlowstatus")
    public ResponseEntity<Object> changeFormFlowstatus(@RequestParam("id") Integer id, @RequestParam("status") String status){
        log.info("修改表格&流程关系表{} 状态：{}",id,status);
        flowFormService.changeFormFlowstatus(id,status);
        return success(true, "修改成功");
    }
    @GetMapping("/edit/{id}")
    public ResponseEntity<Object> editProcess(@PathVariable @NonNull Integer id){
        log.info("编辑流程{}",id);
        Map<String, Object> map = flowFormService.editProcess(id);
        return success(true, map);
    }
    
}
